package com.nuanshui.heatedloan.util.dwz;

/**
 * navTabAjaxDone是DWZ框架中预定义的表单提交回调函数． 服务器转回navTabId可以把那个navTab标记为reloadFlag=1,
 * 下次切换到那个navTab时会重新载入内容. callbackType如果是closeCurrent就会关闭当前tab
 * 只有callbackType="forward"时需要forwardUrl值 如果表单提交只提示操作是否成功, 就可以不指定回调函数.
 * 框架会默认调用DWZ.ajaxDone()
 * 
 * <form action="/user.do?method=save"
 * onsubmit="return validateCallback(this, navTabAjaxDone)">
 * form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作.
 * statusCode=DWZ.statusCode.error表示操作失败, 提示错误原因.
 * statusCode=DWZ.statusCode.timeout表示session超时，下次点击时跳转到DWZ.loginUrl
 * {"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi",
 * "forwardUrl":"", "callbackType":"closeCurrent"} {"statusCode":"300",
 * "message":"操作失败"} {"statusCode":"301", "message":"会话超时"}
 * 
 */
public class AjaxObject {
	// 状态码 ok:200, error:300, timeout:301
	public final static int STATUS_CODE_SUCCESS = 200;
	public final static int STATUS_CODE_FAILURE = 300;
	public final static int STATUS_CODE_TIMEOUT = 301;
	public final static int STATUS_CODE_FORBIDDEN = 403;

	// callbackType类型
	public final static String CALLBACK_TYPE_CLOSE_CURRENT = "closeCurrent";
	public final static String CALLBACK_TYPE_FORWARD = "forward";

	// navTabId的前缀
	public final static String NAVTABID_PREFIX = "moduleNav_";

	/**
	 * 状态码
	 */
	private int errorCode = STATUS_CODE_SUCCESS;
	/**
	 * 消息
	 */
	private String message = "";
	/**
	 * navTab的ID
	 */
	private String navTabId = "";
	/**
	 * 指定navTab要跳转的路径
	 */
	private String forwardUrl = "";
	private String rel = "";
	/**
	 * 指定当前tab要进行的操作
	 */
	private String callbackType = CALLBACK_TYPE_CLOSE_CURRENT;

	public AjaxObject() {

	}

	/**
	 * 构造函数 作用：只是往页面上回传消息，并且当前的模态窗口关闭，适合在添加和修改的时候用
	 */
	public AjaxObject(final String message) {
		this.message = message;
	}

	/**
	 * 构造方法 作用：可以指定是否关闭当前的模态窗口，适合在删除的时候用
	 * 
	 * @param message
	 *            回传的消息
	 * @param isClose
	 *            是否关闭当前的模态窗口
	 */
	public AjaxObject(final String message, final boolean isClose) {
		this.message = message;
		if (isClose == false) {
			this.callbackType = "";
		}
	}

	/**
	 * 构造函数
	 */
	public AjaxObject(final int statusCode, final String message,
			final String callbackType) {
		this.errorCode = statusCode;
		this.message = message;
		this.callbackType = callbackType;
	}

	/**
	 * 构造函数 作用：添加或删除的窗口关闭，当前页面刷新
	 * 
	 * @param message
	 *            消息
	 * @param navTabId
	 *            要刷新的navTabId
	 */
	public AjaxObject(final String message, final String navTabId) {
		this.message = message;
		this.navTabId = NAVTABID_PREFIX + navTabId;
	}

	/**
	 * 构造函数
	 */
	public AjaxObject(final int statusCode, final String message,
			final String navTabId, final String forwardUrl, final String rel,
			final String callbackType) {
		this.errorCode = statusCode;
		this.message = message;
		this.navTabId = navTabId;
		this.forwardUrl = forwardUrl;
		this.rel = rel;
		this.callbackType = callbackType;
	}

	/**
	 * 返回 message 的值
	 * 
	 * @return message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * 设置 message 的值
	 * 
	 * @param message
	 */
	public void setMessage(final String message) {
		this.message = message;
	}

	/**
	 * 返回 forwardUrl 的值
	 * 
	 * @return forwardUrl
	 */
	public String getForwardUrl() {
		return forwardUrl;
	}

	/**
	 * 设置 forwardUrl 的值
	 * 
	 * @param forwardUrl
	 */
	public void setForwardUrl(final String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	/**
	 * 返回 rel 的值
	 * 
	 * @return rel
	 */
	public String getRel() {
		return rel;
	}

	/**
	 * 设置 rel 的值
	 * 
	 * @param rel
	 */
	public void setRel(final String rel) {
		this.rel = rel;
	}

	/**
	 * 返回 callbackType 的值
	 * 
	 * @return callbackType
	 */
	public String getCallbackType() {
		return callbackType;
	}

	/**
	 * 设置 callbackType 的值
	 * 
	 * @param callbackType
	 */
	public void setCallbackType(final String callbackType) {
		this.callbackType = callbackType;
	}

	/**
	 * 返回 navTabId 的值
	 * 
	 * @return navTabId
	 */
	public String getNavTabId() {
		return navTabId;
	}

	/**
	 * 设置 navTabId 的值
	 * 
	 * @param navTabId
	 */
	public void setNavTabId(final String navTabId) {
		this.navTabId = navTabId;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	/**
	 * 重写toString
	 * 
	 * @return
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuilder buffer = new StringBuilder();
		buffer.append("{").append("\"errorCode\":\"" + errorCode + "\",")
				.append("\"message\":\"" + message + "\",")
				.append("\"navTabId\":\"" + navTabId + "\",")
				.append("\"rel\":\"" + rel + "\",")
				.append("\"callbackType\":\"" + callbackType + "\",")
				.append("\"forwardUrl\":\"" + forwardUrl + "\"").append("}");
		return buffer.toString();
	}

}
